/**
 * Copyright (c) 2007, Nathan A. Good
 *
 *	All rights reserved.
 *
 *	Redistribution and use in source and binary forms, with or without 
 *	modification, are permitted provided that the following conditions are met:
 *
 *	*	Redistributions of source code must retain the above copyright notice, 
 *		this list of conditions and the following disclaimer.
 *
 *	*	Redistributions in binary form must reproduce the above copyright 
 *		notice, this list of conditions and the following disclaimer in the 
 *		documentation and/or other materials provided with the distribution.
 *
 *	*	Neither the name of the Crown Financial Ministries nor the names of 
 *		its contributors may be used to endorse or promote products derived
 *		from this software without specific prior written permission.
 *
 *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
 *  THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.crown.kings.jsf;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.crown.kings.dao.DBMapper;
import org.crown.kings.dao.InetAddressEntityDAO;
import org.crown.kings.dao.InetAddressEntityDAOImpl;
import org.crown.kings.entities.InetAddressEntity;
import org.crown.kings.entities.InetAddressEntityExample;

/**
 * A bean for handling InetAddresses (e-mail, internet, etc.) in the UI.
 * 
 * @author nagood
 * @version $Id$
 */
public class InetAddressBean extends BaseAddressBean
{
	private static Log logger = LogFactory.getLog(PersonBean.class);

	/**
	 * 
	 * @param id
	 * @return
	 */
	public static List<InetAddressBean> getInetAddressesFor(final int id)
	{
		final List<InetAddressBean> results = new ArrayList<InetAddressBean>();

		final InetAddressEntityDAO dao = new InetAddressEntityDAOImpl(DBMapper.instance().getSqlMapper());
		final InetAddressEntityExample example = new InetAddressEntityExample();

		example.createCriteria().andPersonIdEqualTo(id);

		List<InetAddressEntity> list;

		try {
			list = dao.selectByExample(example);

			if (logger.isDebugEnabled()) {
				logger.debug("Found " + list.size() + " objects");
			}

			for (final InetAddressEntity entity : list) {
				final InetAddressBean bean = new InetAddressBean();
				bean.setAddress(entity.getInetaddress());
				bean.setAddressType(entity.getInetaddressType());
				bean.setPreferred((entity.getIsPreferred() != null) && entity.getIsPreferred().equals("Y"));
				results.add(bean);
			}

		} catch (final SQLException e) {
			logger.error("Error while getting inet addresses", e);
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error while getting inet addresses", ""));
		}

		return results;
	}

	/**
	 * Runs the add action that adds the values from the bean into the persistent
	 * store.
	 * 
	 * @return String value
	 */
	public String doAdd()
	{
		String result = FAILURE;

		/*
		 * TODO: Replace these with command parameters, and use the wipBean to put
		 * in the parameter
		 */
		final PersonWipBean wip = (PersonWipBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
				.get("personWip");

		final InetAddressEntity record = new InetAddressEntity();
		record.setInetaddress(getAddress());
		record.setInetaddressType(getAddressType());
		record.setPersonId(wip.getId());
		record.setCreateUser("ngood");
		record.setCreateDate(new Date());
		record.setUpdateDate(new Date());
		record.setUpdateUser("ngood");
		record.setVersion(1);
		record.setIsPreferred(isPreferred() ? "Y" : "N");

		final InetAddressEntityDAO dao = new InetAddressEntityDAOImpl(DBMapper.instance().getSqlMapper());
		try {
			dao.insert(record);
			result = SUCCESS;
		} catch (final SQLException e) {
			logger.error("Error while adding inet addresses", e);
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error while adding inet addresses", ""));
		}

		logger.debug(result);

		return result;
	}

	/**
	 * Gets a list of inet addresses for the PersonWipBean that is stored in session.  If there
	 * is none stored in session, this will add an error to the FacesContext because it should 
	 * not happen.
	 * @return
	 */
	public List<InetAddressBean> getList()
	{
		/*
		 * TODO: Replace these with command parameters, and use the wipBean to put
		 * in the parameter
		 */
		final PersonWipBean wip = (PersonWipBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
				.get("personWip");
		return InetAddressBean.getInetAddressesFor(wip.getId());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return getAddress();
	}

}
